import java.sql.ResultSet;
import java.sql.SQLException;
public class Main extends Thread
{
    @Override
    public void run()
    {
        System.out.println(this.getName() + " started");
        ResultSet resultSet = null;
        OrderedStatement statement = null;
        try
        {
            statement = Resources.getStatement();
            System.out.println(getName() + " got statement" + statement.getId());
            resultSet = statement.executeQuery("select code from student");
            while (resultSet.next())
                System.out.println(getName() + " got result " +  (resultSet.getLong("code") + (1L << 31) - 1) + " by statement" + statement.getId());
        }
        catch (SQLException throwables)
        {
            throwables.printStackTrace();
        }
        finally
        {
            Resources.toRelease(statement);
            System.out.println(getName() + " ended with releasing statement" + statement.getId());
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException
    {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Class.forName("Resources");
        Resources.testException();
        Main[] users = new Main[5];
        for (int i = 0;i<users.length;++i)
        {
            users[i] = new Main();
            users[i].setName("user" + i);
            users[i].start();
        }
        for (int i = 0;i<users.length;++i)
            users[i].join();
        Resources.close();
    }
}
